//
//  AreaCodeView.m
//  EPTool
//
//  Created by guoshiqing on 2018/5/2.
//  Copyright © 2017年 云数动能. All rights reserved.
//

#import "AreaCodeView.h"

#import "CountryCode.h"
#import "CountryCodeTVC.h"
#import "UIView+DDAddition.h"
#import "VALable.h"
#import "Macro.h"
#import "GlobalUtil.h"


@interface AreaCodeView()

@property (nonatomic, strong) UIButton *btnAreaCode;
@property (nonatomic, strong) UILabel *lblAreaCode;
@property (nonatomic, strong) UIImageView *igvAreaCode;

// 保存所有国家地区码
@property (nonatomic, strong) NSMutableDictionary *dicCode;

@end

@implementation AreaCodeView

#pragma mark - 懒加载
- (UIButton *)btnAreaCode
{
    if (!_btnAreaCode) {
        _btnAreaCode = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _btnAreaCode.backgroundColor = [UIColor clearColor];
        [_btnAreaCode addTarget:self action:@selector(btnAreCodeClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnAreaCode;
}

- (UILabel *)lblAreaCode
{
    if (!_lblAreaCode) {
        _lblAreaCode = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
    return _lblAreaCode;
}

- (UIImageView *)igvAreaCode
{
    if (!_igvAreaCode) {
        _igvAreaCode = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24.0f, 24.0f)];
        _igvAreaCode.image = [UIImage imageNamed:@"icon_pulldown"];
    }
    return _igvAreaCode;
}


#pragma mark - 懒加载
- (NSMutableDictionary *)dicCode
{
    if (!_dicCode) {
        _dicCode = [NSMutableDictionary dictionary];
    }
    return _dicCode;
}


#pragma mark - 加载国家地区码
- (void)loadCountryCode
{
    CountryCode *code = [[CountryCode alloc] init];
    self.dicCode = code.dicCountryCodes;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame withDefaultAreaCode:(NSString *)areaCode
{
    self = [super initWithFrame:frame];
    self.frame = frame;
    if (self) {
        [self addSubViews];
        [self setSubViewsWithAreaCode:areaCode];
    }
    return self;
}

- (void)addSubViews
{
    [self addSubview:self.btnAreaCode];
    [self.btnAreaCode addSubview:self.lblAreaCode];
    [self.btnAreaCode addSubview:self.igvAreaCode];
    self.igvAreaCode.centerY = self.lblAreaCode.centerY - 2.0f;
}

- (void)setSubViewsWithAreaCode:(NSString *)areaCode
{
    // 获取国家地区码
    [self loadCountryCode];
    
    // 设置国家地区码的图标
    self.lblAreaCode.font = SystemFont(18.0f);
    
    // 填充默认国家地区码
    if (nil == areaCode || 0 == areaCode.length) {
        self.strAreaCode = @"86";
    }
    else
    {
        self.strAreaCode = areaCode;
    }
    
    self.lblAreaCode.text = [NSString stringWithFormat:@"+%@", self.strAreaCode];
    [self reSetBtnFrame];
}

- (void)reSetBtnFrame
{
    CGSize size = [GlobalUtil sizeFortext:self.lblAreaCode.text withFont:self.lblAreaCode.font constrainedToSize:CGSizeMake(200.0f, self.frame.size.height)];
    self.lblAreaCode.width = size.width;
    self.btnAreaCode.width = self.lblAreaCode.width + self.igvAreaCode.width;
    self.width = self.btnAreaCode.width;
    self.igvAreaCode.left = self.lblAreaCode.right + 2.0f;
}



#pragma mark - 点击事件
- (void)btnAreCodeClick:(UIButton *)btnAreaCode
{
    CountryCodeTVC *countryCodeTVC = [[CountryCodeTVC alloc] init];
    countryCodeTVC.dicCode = self.dicCode;
    __weak typeof(self) weakSelf = self;
    countryCodeTVC.completion = ^(CountryModel *model) {

        weakSelf.lblAreaCode.text = model.dial_code;
        

        weakSelf.strAreaCode = [model.dial_code substringFromIndex:1];
        weakSelf.strAreaCode = [weakSelf.strAreaCode stringByReplacingOccurrencesOfString:@" " withString:@""];
        [weakSelf reSetBtnFrame];
        
        if (self.selectBlock) {
            self.selectBlock(weakSelf.strAreaCode, model.name);
        }
    };
    
    // UIView+DDAddition中viewController方法可以获得当前view所在的控制器
    if ([self viewController].navigationController) {
        [[self viewController].navigationController pushViewController:countryCodeTVC animated:YES];
    }
}


//#pragma mark - 根据国家地区码返回国家地区（不能用NSMutableDictionary+PhoneZone里的类，会黑幕）
//- (NSString *)countryNameWithCode:(NSString *)code
//{
//    // 入参处理
//    NSString *strCode = [code stringByReplacingOccurrencesOfString:@" " withString:@""];
//    strCode  = [strCode stringByReplacingOccurrencesOfString:@"+" withString:@""];
//
//    // 喜欢匹配
//    NSString *strTmp = @"";
//    NSArray *listCountry = nil;
//    for (NSString *key in self.dicCode.allKeys) {
//
//        listCountry = self.dicCode[key];
//        for (CountryModel *model in listCountry) {
//            strTmp  = [model.dial_code stringByReplacingOccurrencesOfString:@" " withString:@""];
//            strTmp  = [strTmp stringByReplacingOccurrencesOfString:@"+" withString:@""];
//            if ([strTmp isEqualToString:strCode]) {
//                return [NSString stringWithFormat:@"%@", model.name];
//            }
//        }
//    }
//    return @"国家代码无效";
//}

#pragma mark - 设置文字的颜色
- (void)setTextColor:(UIColor *)textColor
{
    self.lblAreaCode.textColor = textColor;
}

@end
